﻿using System.Collections.Generic;
using Dapper;
using Microsoft.Data.SqlClient;
using Oracle.ManagedDataAccess.Client;

namespace dataXJobGenerator
{
    public interface IDBColumnsUtil
    {
        (int records, List<string> columns) GetColumns(string connectionString, string tableName);
    }

    public class SQLServerDBColumnsUtil:IDBColumnsUtil
    {
        public (int records, List<string> columns) GetColumns(string connectionString, string tableName)
        {
            using (var connection = new SqlConnection(connectionString))
            {
                var count = connection.QueryFirst<int>($"Select count(0) records from {tableName}");
                var columns = connection
                    .Query<string>(
                        $"select COLUMN_NAME from information_schema.COLUMNS where table_name ='{tableName}'")
                    .AsList();
                if (columns.Contains("RECORD_ID")) columns.Remove("RECORD_ID");

                return (count, columns);
            }
        }
    }
    public class OracleDBColumnsUtil:IDBColumnsUtil
    {
        public (int records, List<string> columns) GetColumns(string connectionString, string tableName)
        {
           
            using (var connection = new OracleConnection(connectionString))
            {
                var columns = connection
                    .Query<string>($"SELECT COLUMN_NAME FROM COLS WHERE TABLE_NAME='{tableName}'")
                    .AsList();
                var count = connection.QueryFirst<int>($"Select count(0) records from {tableName}");

                return (count, columns);
            }
        }
    }
}